package com.pro.controller;

import com.pro.dao.DianzanStatusMapper;
import com.pro.dao.EvaluationMapper;
import com.pro.dao.MemberMapper;
import com.pro.domain.DianzanStatus;
import com.pro.domain.Evaluation;
import com.pro.domain.Member;
import com.pro.domain.MemberReadState;
import com.pro.service.DianzanStatusService;
import com.pro.service.MemberService;
import com.pro.service.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MemberController {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberService memberService;
    @Autowired
    private DianzanStatusService dianzanStatusService;
    @Autowired
    private EvaluationMapper evaluationMapper;

    @GetMapping("/register")
    public ModelAndView genRegister(){
        return new ModelAndView("/reg");
    }

    @GetMapping("/login")
    public ModelAndView goLoginPage(){
        return new ModelAndView("/login");
    }

    @RequestMapping("/addMember")
    @ResponseBody
    public String addMember (Member member, String vc, HttpSession session){
        Object verifyCode = session.getAttribute("verifyCode");
        if(vc.equals(verifyCode)){
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            member.setCreateTime(timestamp);
            member.setSalt(0);
            memberMapper.insert(member);
            return "success";
        }
        return "1";
    }

    /**
     * 注册接口
     * @param vc
     * @param username
     * @param password
     * @param nickname
     * @param request
     * @return
     */
    @PostMapping("/regist")
    @ResponseBody
    public Map regist(String vc, String username, String password, String nickname, HttpServletRequest request){
        //
        String verifyCode = (String) request.getSession().getAttribute("verifyCode");
        Map result=new HashMap();
        if(vc==null||verifyCode==null||!vc.equalsIgnoreCase(verifyCode)){  //vc.equalsIgnoreCase(verifyCode)忽略大小写
            result.put("code","vc01");//这个码，团队小组有文档标识是什么意思。
            result.put("msg","验证码错误");
        }else{
            try {
                Member member = memberService.createMember(username, password, nickname);
                result.put("code","0");//这个码，团队小组有文档标识是什么意思。
                result.put("msg","success");
                result.put("data",member); //如果前端需要，你就加上
            } catch (ServiceException e) {
                e.printStackTrace();
                result.put("code",e.getCode()); //这个码，团队小组有文档标识是什么意思。
                result.put("code",e.getMsg());
                result.put("data",null);
            }
        }
        return result;
    }

    @PostMapping("/checkLogin")
    @ResponseBody
    public Map checkLogin(String username,String password,String vc,HttpSession session){
        String verifyCode = (String) session.getAttribute("verifyCode");
        Map result=new HashMap();
        if(vc==null||verifyCode==null||!vc.equalsIgnoreCase(verifyCode)){
            result.put("code","vc01");//这个码，团队小组有文档标识是什么意思。
            result.put("msg","验证码错误");
        }else{
            try {
                Member member = memberService.checkLogin(username, password);
                if(member!=null){
                    session.setAttribute("loginMember",member);
                    result.put("code","0");
                    result.put("msg","success");
                    result.put("member",member);
                }
            } catch (ServiceException e) {
                e.printStackTrace();
                result.put("code",e.getCode());
                result.put("msg",e.getMsg());
            }
        }
        return result;
    }

    /**
     * 更新阅读状态
     * @param bookId
     * @param memberId
     * @param readState
     * @return
     */
    @RequestMapping("/updateReadState")
    @ResponseBody
    public Map updateReadState(Long bookId,Long memberId,Integer readState){
        Map result=new HashMap();
        try {
            MemberReadState memberReadState = memberService.updateMemberReadState(bookId, memberId, readState);
            result.put("code",0);
            result.put("msg","success");
        } catch (ServiceException e) {
            e.printStackTrace();
            result.put("code",e.getCode());
            result.put("msg",e.getMsg());
        }
        return result;
    }

    @RequestMapping("/evaluate")
    @ResponseBody
    public Map evaluation(Long memberId,Long bookId,Integer score,String content){
        Map result=new HashMap();
        try {
            Evaluation evaluate = memberService.evaluate(memberId, bookId, score, content);
            result.put("code","0");
            result.put("msg","success");
            result.put("data",evaluate);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.put("code",e.getCode());
            result.put("msg",e.getMsg());
        }
        return result;
    }

    /*@PostMapping("/enjoy")
    @ResponseBody
    public Map enjoy(Long evaluationId){
        Map result=new HashMap();
        try {
            Evaluation evaluation = memberService.enjoy(evaluationId);
            result.put("code","0");
            result.put("msg","success");
            result.put("data",evaluation);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.put("code",e.getCode());
            result.put("msg",e.getMsg());
        }
        return result;
    }*/

    @PostMapping("/enjoy")
    @ResponseBody
    public Map enjoy(Long evaluationId,Long memberId){
        Map result=new HashMap();
        try {
            dianzanStatusService.changeDianzanStatus(evaluationId,memberId);
            Evaluation evaluation = evaluationMapper.selectById(evaluationId);
            result.put("code","0");
            result.put("msg","success");
            result.put("data",evaluation);
        } catch (ServiceException e) {
            e.printStackTrace();
            result.put("code",e.getCode());
            result.put("msg",e.getMsg());
        }
        return result;
    }

}
